<template>
  <div class="my-questions">
    <h2>我的问答</h2>

    <!-- 问题列表 -->
    <div class="questions-list" v-loading="loading">
      <el-empty v-if="questions.length === 0" description="暂无问答记录" />
      
      <el-card v-for="question in questions" 
        :key="question.issueId" 
        class="question-card"
        shadow="hover"
      >
        <div class="question-header">
          <span class="question-time">{{ question.createTime }}</span>
          <el-tag :type="question.status === 1 ? 'success' : 'warning'">
            {{ question.status === 1 ? '已回答' : '待回答' }}
          </el-tag>
        </div>

        <p class="question-content">{{ question.text }}</p>
        
        <div class="question-footer">
          <div class="question-meta">
            <span>
              <el-icon><ChatRound /></el-icon>
              {{ question.messageCount || 0 }}条回复
            </span>
          </div>
          <el-button type="primary" link @click="viewDetail(question)">
            查看详情
            <el-icon class="el-icon--right"><ArrowRight /></el-icon>
          </el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ChatRound, ArrowRight } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getMessageBoardList, type MessageBoard } from '../../api/messageboard'

const router = useRouter()
const loading = ref(false)
const questions = ref<MessageBoard[]>([])

// 获取我的问答列表
const fetchMyQuestions = async () => {
  loading.value = true
  try {
    const userId = localStorage.getItem('userid')
    if (!userId) {
      ElMessage.error('获取用户信息失败')
      return
    }
    
    const res = await getMessageBoardList()
    console.log('问答列表响应:', res)
    if (res.code === '200') {
      // 过滤出当前用户的问题
      questions.value = res.data.filter(q => q.createrId === Number(userId))
    } else {
      ElMessage.error(res.msg || '获取问答列表失败')
    }
  } catch (error) {
    console.error('获取问答列表失败:', error)
    ElMessage.error('获取问答列表失败')
  } finally {
    loading.value = false
  }
}

// 查看问题详情
const viewDetail = (question: MessageBoard) => {
  router.push(`/message-board/${question.issueId}`)
}

onMounted(() => {
  fetchMyQuestions()
})
</script>

<style scoped>
.my-questions {
  padding: 20px;
}

h2 {
  margin: 0 0 30px;
  font-size: 24px;
  color: #2c3e50;
}

.question-card {
  margin-bottom: 20px;
}

.question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.question-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.question-tag {
  background-color: #e8f4ff;
  color: #409EFF;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.question-time {
  color: #909399;
  font-size: 12px;
}

.question-status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
}

.question-status.answered {
  background-color: #f0f9eb;
  color: #67c23a;
}

.question-status.pending {
  background-color: #fdf6ec;
  color: #e6a23c;
}

.question-title {
  font-size: 16px;
  color: #2c3e50;
  margin: 0 0 12px;
  line-height: 1.4;
}

.question-content {
  color: #666;
  font-size: 14px;
  line-height: 1.6;
  margin: 12px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.question-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 16px;
}

.question-meta {
  display: flex;
  gap: 16px;
  color: #909399;
  font-size: 13px;
}

.question-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.pagination-container {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

/* 详情对话框样式 */
.question-detail {
  padding: 20px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.detail-title {
  font-size: 20px;
  color: #2c3e50;
  margin: 0 0 16px;
}

.detail-content {
  color: #666;
  line-height: 1.8;
  margin-bottom: 30px;
}

.answers-section h4 {
  font-size: 16px;
  color: #2c3e50;
  margin: 0 0 20px;
}

.answer-item {
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
  margin-bottom: 16px;
}

.answer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.answerer-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.answerer-name {
  color: #409EFF;
  font-weight: 500;
}

.answer-time {
  color: #909399;
  font-size: 12px;
}

.answer-content {
  color: #606266;
  line-height: 1.6;
  margin: 0;
}
</style> 